<?php

/**
 * +------ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER * ---------+
 * +----------------------------------------------------------------------------+
 * | PHPKiisf - Keep It in Simple Framework					|
 * | Version 1.0								|
 * | Copyright (C) <2013>  Mario Cezzare <mcezzare@gmail.com>			|
 * | Blog: http://mcezzare.blogspot.com.br					|
 * |										|
 * | This program is part of PHPKiisf, and is free software: you can		|
 * | redistribute it and/or modify it under the terms of the GNU 		|
 * | General Public License as published by					|
 * | the Free Software Foundation, either version 3 of the License, or		|
 * | (at your option) any later version.					|					|
 * |										|									|
 * | This program is distributed in the hope that it will be useful,		|
 * | but WITHOUT ANY WARRANTY; without even the implied warranty of		|
 * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the		|
 * | GNU General Public License for more details.				|
 * | You should have received a copy of the GNU General Public License		|
 * | along with this program.  If not, see <http://www.gnu.org/licenses/>.	|
 * +----------------------------------------------------------------------------+
 * | Author : <Mario Cezzare Angelicola Chiodi> <mcezzare@gmail.com>		|
 * +----------------------------------------------------------------------------+
 * | Contributors								|
 * |										|
 * +----------------------------------------------------------------------------+
 *
 */
/**
 * This file is named phpkiisf_controller, but works as a config_controller for the framework<br />
 * if you need write a config controller create your own or extend this.
 *
 * @package	controllers
 * @access	public
 * @since	2013-03-08
 * @author	Mario Cezzare <mcezzare@gmail.com>
 */

/**
 * Common methods to posibility the framework to :
 * @todo - Create method scaffold to create business models/classes from db tables
 *
 */
class phpkiisf_controller extends default_controller {

	public static $_info = array() ;
	private static $_default_config ;
	private static $_default_template_section ;

	public function __construct() {
		base_template::init() ;
		self::$_default_config = config::get_config() ;
		/* defined in phpkiisf.ini, Section TEMPLATES */
		self::$_default_template_section = "CONFIG_HOME" ;
	}

	/**
	 * @author [Mario Cezzare Angelicola Chiodi] <mcezzare@gmail.com>
	 * @since date yyyy-mm-dd
	 */
	public static function init() {
		echo "oi" ;
	}

	/**
	 * [display_config description]
	 *
	 * @param  [type] $options [description]
	 * @return [type]          [description]
	 */
	public static function display_config( $options = null ) {

		$include_file = self::get_template( self::$_default_template_section ) ;
		/* If exists,  include the template, defined in phpkiisf.ini */
		if ( !self::include_template( $include_file ) ) {

		}
	}

	/**
	 * [test_config description]
	 * @param  [type] $options [description]
	 * @return [type]          [description]
	 *
	 * @author [author] <[email]>
	 */
	public static function test_config( $options = null ) {
		$include_file = self::get_template( self::$_default_template_section ) ;
		/* If exists,  include the template, defined in phpkiisf.ini */
		$params = array(
			"test" => true ,
			"uri" => "/test" ,
			) ;
		if ( !self::include_template( $include_file , $params ) ) {

		}
	}

	public static function display_about( $options = null ) {
		$include_file = self::get_template( self::$_default_template_section ) ;
		$params = array(
			"alternative_file" => "pages/about" ,
			"uri" => "/about" ,
			"section_name" => "About" ,
			) ;
//				var_dump( $include_file ) ;
		/* If exists,  include the template, defined in phpkiisf.ini
		 * informing the alternative file to load
		 */
		if ( !self::include_template( $include_file , $params ) ) {

		}
	}

	public function display_test_db_config() {

		$include_file = self::get_template( self::$_default_template_section ) ;

		$params = array(
			"alternative_file" => "pages/test" ,
			"uri" => "/config/db" ,
			"section_name" => "Database Test" ,
			) ;
		/* If exists,  include the template, defined in phpkiisf.ini
		 * informing the alternative file to load
		 */
		if ( !self::include_template( $include_file , $params ) ) {

		}
	}

	/**
	 * '
	 * @param type $options
	 */
	public static function test_db_config( $options = null ) {
		$ready = false ;
		$ready = self::check_defined( DB_TYPE , "DB_TYPE" ) ;
		$ready = self::check_defined( DB_HOST , "DB_HOST" ) ;
		$ready = self::check_defined( DB_USER , "DB_USER" ) ;
		$ready = self::check_defined( DB_PASS , "DB_PASS" ) ;
		$ready = self::check_defined( DB_NAME , "DB_NAME" ) ;

		if ( $ready ) {
			$connection = new connection( "MYSQL" ) ;

			if ( $connection->__get( "status" ) == 1 ) {
				$message = new message() ;
				$message = utils::prepare_message(
						array(
							"MSG_TYPE" => "INFO" ,
							"OPERATION" => "CONNECT DB" ,
							"DETAIL" => "Conected Sucefully." ,
							"OBS" => "" ,
						)
					) ;
			} else {
				$message = utils::prepare_message(
						array(
							"MSG_TYPE" => "ERROR" ,
							"OPERATION" => "CONNECT DB" ,
							"DETAIL" => "Can't connect DB." ,
							"OBS" => "" ,
						)
					) ;
			}
			echo "<div class='container'><p>" ;

			$array = config::get_config( "DATABASE" ) ;
//						var_dump( $array ) ;
			utils::dump_array( array( "DATABASE" => $array ) , array( "test" => true ) ) ;
			utils::inform_user( $message ) ;

			gui_controller::create_button( array(
				"value" => "voltar" ,
				"href" => "javascript:history.go(-1);"
			) ) ;
			echo "</p></div>" ;
		}
	}

	/**
	 * Called by route /builder
	 *
	 * @param array $options
	 */
	public function display_builder( $options = null ) {

		$include_file = self::get_template( self::$_default_template_section ) ;

		$params = array(
			"alternative_file" => "pages/builder" ,
			"uri" => "/builder/" ,
			"section_name" => "MVC Builder" ,
			) ;
		/* If exists,  include the template, defined in phpkiisf.ini
		 * informing the alternative file to load
		 */
		if ( !self::include_template( $include_file , $params ) ) {

		}
	}

	/**
	 * Called by route /builder
	 *
	 * @param array $options
	 */
	public function display_prepare_uml( $options = null ) {
		// $uml			 = new phpuml();
		// $start_folder	 = $_SERVER[ "DOCUMENT_ROOT" ];
		// $start_folder	 	 = substr( $start_folder	  , 0 , strlen( $start_folder	  ) - 1 );
		// $uml->start_path = $start_folder	 ;

		$include_file = self::get_template( self::$_default_template_section ) ;

		$params = array(
			"alternative_file" => "pages/builder_uml" ,
			"uri" => "/builder/uml" ,
			"section_name" => "UML Builder" ,
			) ;
		/* If exists,  include the template, defined in phpkiisf.ini
		 * informing the alternative file to load
		 */
		if ( !self::include_template( $include_file , $params ) ) {

		}
		// printf( "Reading %s <pre>" , $uml->start_path );
		// print_r( $uml->get_list_files( false ) );
		// echo "-----------------------------------------------------\n";
		// echo "</pre>";
	}

	/**
	 * Verify if a constant is defined
	 *
	 * @param String $constant
	 * @param String $constant_name
	 * @param boolean $print if will show
	 * @return boolean
	 */
	private static function check_defined( $constant , $constant_name , $print = false ) {
		if ( is_null( $constant ) ) {
			$print ? printf( "%s not defined<br>" , $constant ) : null ;
			return false ;
		} else {
			$print ? printf( "%s defined,Value: %s<br>" , $constant_name , $constant ) : null ;
			return true ;
		}
	}

	/**
	 * @override - extends from default, change here for your new home
	 * rewrited to  /docs
	 *
	 * @param type $options
	 * @return boolean
	 * @obs changed from home
	 */
	public static function display_index( $options = array() ) {
		/* to get templates list  */
		self::$_default_template_section = "DOCS_HOME" ;
		$include_file = self::get_template( self::$_default_template_section ) ;
		/* If exists,  include the template, defined in phpkiisf.ini */
		if ( !self::include_template( $include_file , $options ) ) {

		}
		printf( "Este Arquivo:#%s" , $include_file ) ;
	}

}

?>